<?php
/**
 +------------------------------------------------------------------------------
 * 分页显示类 ，扩展用于通过js方式调用分页请求。
 * 页面上需实现，".$this->goPageFun."(pno) js函数。参数为页数。
 +------------------------------------------------------------------------------
 * @category   ORG
 * @package  ORG
 * @subpackage  Util
 * @version   $Id$
 +------------------------------------------------------------------------------
 */
class KPage extends Think
{//类定义开始

    /**
     +----------------------------------------------------------
     * 分页起始行数
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
    protected $firstRow ;

    /**
     +----------------------------------------------------------
     * 列表每页显示行数
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
    protected $listRows ;

    /**
     +----------------------------------------------------------
     * 页数跳转时要带的参数
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
    protected $parameter  ;

    /**
     +----------------------------------------------------------
     * 分页总页面数
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
    protected $totalPages  ;

    /**
     +----------------------------------------------------------
     * 总行数
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
    protected $totalRows  ;

    /**
     +----------------------------------------------------------
     * 当前页数
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
    protected $nowPage    ;

    /**
     +----------------------------------------------------------
     * 分页的栏的总页数
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
    protected $coolPages   ;

    /**
     +----------------------------------------------------------
     * 分页栏每页显示的页数
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
    protected $rollPage   ;

    /**
     +----------------------------------------------------------
     * js分页函数名
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
    protected $goPageFun  ;
    
    /**
     +----------------------------------------------------------
     * 分页记录名称
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */

    // 分页显示定制
    protected $config   =   array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页');
    
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }
    
    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $firstRow  起始记录位置
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
    public function __construct($totalRows,$listRows='',$rollPage=5,$parameter='')
    {		$this->rollPage = $rollPage;
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        $this->listRows = !empty($listRows)?$listRows:20;
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        $this->nowPage  = !empty($_REQUEST['pno'])&&($_REQUEST['pno'] >0)?$_REQUEST['pno']:1;
		$this->goPageFun= !empty($_REQUEST['goPageFun'])?$_REQUEST['goPageFun']:'goPage';
        if( (!empty($this->totalPages) && $this->nowPage>$this->totalPages) || $_REQUEST['pno']=='last' ) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }
		
		

    

    /**
     +----------------------------------------------------------
     * 分页显示
     * 用于在页面显示的分页栏的输出
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    public function show($isArray=false){

        if(0 == $this->totalRows) return;
        $nowCoolPage      = ceil($this->nowPage/$this->rollPage);
		
        //上下翻页字符串
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
        if ($upRow>0){
            $upPage="<span><a href='javascript:".$this->goPageFun."($upRow)'>".$this->config['prev']."</a></span>";
        }else{
            $upPage='<span class=disabled"><font>'.$this->config['prev'].' </font></span>';
        }

        if ($downRow <= $this->totalPages){
            $downPage="<span><a href='javascript:".$this->goPageFun."($downRow)'>".$this->config['next']."</a></span>";
        }else{
            $downPage='<span class=disabled"><font>'.$this->config['next'].'</font></span>';
        }
        
        // << < > >>
        if($nowCoolPage == 1){
            $theFirst = "";
            $prePage = "";
        }else{
            $preRow =  $this->nowPage-$this->rollPage;
            $prePage = "<span><a href='javascript:".$this->goPageFun."($preRow)' >上".$this->rollPage."页</a></span>";
            $theFirst = "<span><a href='javascript:".$this->goPageFun."(1)' >1</a></span>...";
        }
        if($nowCoolPage == $this->coolPages){
            $nextPage = "";
            $theEnd="";
        }else{
            $nextRow = $this->nowPage+$this->rollPage;
            $theEndRow = $this->totalPages;
            $nextPage = "<span><a href='javascript:".$this->goPageFun."($nextRow)' >下".$this->rollPage."页</a></span>";
            $theEnd = "...<span><a href='javascript:".$this->goPageFun."($theEndRow)' >{$theEndRow}</a></span>";
        }
        // 1 2 3 4 5
        $linkPage = "";
        for($i=1;$i<=$this->rollPage+1;$i++){
            $page=($nowCoolPage-1)*$this->rollPage+$i;
            if($page!=$this->nowPage){
                if($page<=$this->totalPages){
                    $linkPage .= "<span><a href='javascript:".$this->goPageFun."($page)'>".$page."</a></span>";
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= "<span id='page_on'>".$page."</span>";
                }
            }
        }
        
        //总数
		//$countPage = '<span><font>'. $this->nowPage .'/共'. $this->totalPages .'页</font></span>';
		//$countPage = '<span><font>'. $this->nowPage .'/'. $this->totalPages .'</font></span>';
		$pageStr = $upPage.$theFirst.$linkPage.$theEnd.$downPage.$countPage;
		if($this->totalPages <= 1) return '';
        /**
        if($isArray) {
            $pageArray['totalRows'] =   $this->totalRows;
            $pageArray['upPage']    =   $url.'&'.'pno'."=$upRow";
            $pageArray['downPage']  =   $url.'&'.'pno'."=$downRow";
            $pageArray['totalPages']=   $this->totalPages;
            $pageArray['firstPage'] =   $url.'&'.'pno'."=1";
            $pageArray['endPage']   =   $url.'&'.'pno'."=$theEndRow";
            $pageArray['nextPages'] =   $url.'&'.'pno'."=$nextRow";
            $pageArray['prePages']  =   $url.'&'.'pno'."=$preRow";
            $pageArray['linkPages'] =   $linkPage;
            $pageArray['nowPage'] =   $this->nowPage;
            return $pageArray;
        }
        **/
        return $pageStr;
    }

}//类定义结束
?>
